package com.ly.honor.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.honor.entity.Clue;
import com.ly.honor.entity.Match;
import com.ly.honor.entity.dto.MatchCreateDTO;
import com.ly.honor.entity.dto.MatchEditDTO;
import com.ly.honor.entity.dto.MatchQueryDTO;
import com.ly.honor.entity.vo.MatchQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.ClueMapper;
import com.ly.honor.service.MatchService;
import com.ly.honor.mapper.MatchMapper;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
* @author 17798
* @description 针对表【bs_match】的数据库操作Service实现
* @createDate 2022-11-16 11:49:49
*/
@Service
public class MatchServiceImpl extends ServiceImpl<MatchMapper, Match>
    implements MatchService{

    @Autowired
    private MatchMapper matchMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Override
    public Page<MatchQueryVO> selectMatchPage(long pageNum, long pageSize, MatchQueryDTO matchQueryDTO) {
        var page = new Page<MatchQueryVO>(pageNum,pageSize);
        Page<MatchQueryVO> matchQueryVOPage = matchMapper.selectMatchPage(page,matchQueryDTO);

        return matchQueryVOPage;
    }

    @Override
    public MatchQueryVO getMatchById(long id) {
        Match match = matchMapper.selectById(id);
        if(ObjectUtils.isEmpty(match)){
            throw new HonorException(ResultEnum.MESSAGE_INVALID);
        }
        var matchQueryVO = new MatchQueryVO();
        BeanUtils.copyProperties(match,matchQueryVO);
        var clueIda = matchQueryVO.getClueIda();
        var clueIdb = matchQueryVO.getClueIdb();
        var clueA = clueMapper.selectById(clueIda);
        var clueB = clueMapper.selectById(clueIdb);
        if(!ObjectUtils.isEmpty(clueA)){
            matchQueryVO.setClueIdaName(clueA.getName());
        }
        if(!ObjectUtils.isEmpty(clueB)){
            matchQueryVO.setClueIdbName(clueB.getName());
        }
        return matchQueryVO;
    }

    @Override
    public void insertMatch(MatchCreateDTO matchCreateDTO) {
        if(ObjectUtils.isEmpty(matchCreateDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var clueIda = matchCreateDTO.getClueIda();
        var clueIdb = matchCreateDTO.getClueIdb();
        if(clueIda.equals(clueIdb)){
            throw new HonorException("两支队伍相同，请重新选择");
        }
        var clueA = clueMapper.selectById(clueIda);
        var clueB = clueMapper.selectById(clueIdb);
        if(ObjectUtils.isEmpty(clueA) || ObjectUtils.isEmpty(clueB) ){
            throw new HonorException(ResultEnum.MESSAGE_INVALID);
        }
        var match = new Match();
        BeanUtils.copyProperties(matchCreateDTO,match);
        matchMapper.insert(match);
    }

    @Override
    public void updateMatch(MatchEditDTO matchEditDTO) {
        if(ObjectUtils.isEmpty(matchEditDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var clueIda = matchEditDTO.getClueIda();
        var clueIdb = matchEditDTO.getClueIdb();

        var clueA = clueMapper.selectById(clueIda);
        var clueB = clueMapper.selectById(clueIdb);
        if(ObjectUtils.isEmpty(clueA) || ObjectUtils.isEmpty(clueB) ){
            throw new HonorException(ResultEnum.MESSAGE_INVALID);
        }
        var match = new Match();
        BeanUtils.copyProperties(matchEditDTO,match);
        matchMapper.updateById(match);
    }

    @Override
    public List<MatchQueryVO> getMatchByTime(String time) {
        Date date = DateUtil.parse(time, "yyyy-MM-dd");
        Date beginTime = DateUtil.beginOfDay(date);
        Date endTime = DateUtil.endOfDay(date);
        List<MatchQueryVO> matchQueryVOList = matchMapper.getMatchByTime(beginTime,endTime);
        for (MatchQueryVO matchQueryVO:matchQueryVOList){
            var matchTimeSpot = DateUtil.format(matchQueryVO.getMatchTime(), "HH:mm");
            matchQueryVO.setMatchTimeSpot(matchTimeSpot);
        }
        return matchQueryVOList;
    }
}




